clear all

log using "E:\workdata\702992\702992\Francesco\institutional trust\replication\output/replicate_main_analysis.log"


****** Global definitions
 
 * outcomes of interest
global outcomes  satisfaction_democracy trust_state_inst trust_pol_inst

* distances
global distance 100 250 1000

* inequality measures
global ivs giniadj giniadj_car giniadj_wealth1 

* reset frames
frames reset

* load data and specify panel structure
use "E:\workdata\702992\702992\Francesco\data\clean\replication_data", replace
xtset id counter_panel 

* create frame to store regression outputs
frame create output str35(dv) str35(variable) str35(model) distance b se ub95 lb95 ub90 lb90 n

**************************************************************
*************** Regression oputput for Table 2 ***************
**************************************************************

* Cross-section analysis
foreach dv in $outcomes {
	
	reg `dv' giniadj_100 /// outcome and IV
			age i.male i.danish_origin i.citizen income_log i.unemployed i.civil_status i.living_status years_at_address education_years i.survey /// individual controls
			tot_neighbors_100 agespread_100 median_income_100 unempl_narrow_100 residential_turnover_100 ethnic_diversity_100 single_parents_100 /// contextual controls
			if sample==1, /// sample restrictions
			vce(robust)
			
}

* Panel analysis
foreach dv in $outcomes {
	
	* Limit the analisis to individuals interviewed in both waves
	quietly: xtreg `dv' giniadj_100 /// outcome and IV
			i.citizen  i.unemployed income_log i.civil_status i.living_status years_at_address education_years i.counter_panel /// time varying individual controls
			tot_neighbors_100 agespread_100 median_income_100 unempl_narrow_100 residential_turnover_100 ethnic_diversity_100 single_parents_100 /// contextual controls
			if sample==1 & p2ws==1, /// sample restrictions
				fe ///
				cluster(id)
				
	tempvar countid
	egen `countid'=count(id) if e(sample)==1, by(id)

	xtreg `dv' giniadj_100 /// outcome and IV
			i.citizen  i.unemployed income_log i.civil_status i.living_status years_at_address education_years i.counter_panel /// time varying individual controls
			tot_neighbors_100 agespread_100 median_income_100 unempl_narrow_100 residential_turnover_100 ethnic_diversity_100 single_parents_100 /// contextual controls
			if sample==1 & p2ws==1 & `countid'==2, /// sample restrictions
				fe ///
				cluster(id)

}


****************************************************************************
*************** Regression oputput used for Fiugure 2 & 3. *****************
*************** Individual models shown in Tables F.1 to F.9 ***************
****************************************************************************

* run all the analyses needed for Figure 2 and 3
foreach dv in $outcomes {
		
	foreach d in 100 250 1000{
		
		foreach iv in giniadj_`d'_st giniadj_car_`d'_st giniadj_wealth1_`d'_st{

	
****** Cross-section

			reg `dv' `iv' /// outcome and IV
				age i.male i.danish_origin i.citizen income_log i.unemployed i.civil_status i.living_status years_at_address education_years i.survey /// individual controls
				tot_neighbors_`d' agespread_`d' median_income_`d' unempl_narrow_`d' residential_turnover_`d' ethnic_diversity_`d' single_parents_`d' /// contextual controls
				if sample==1, /// sample restrictions
				vce(robust)

			
			frame post output ("`dv'") ("`iv'") ("Cross-section") (`d') (_b[`iv']) (_se[`iv']) (_b[`iv']-invttail(e(df_r),.975)*_se[`iv']) (_b[`iv']+invttail(e(df_r),.975)*_se[`iv']) (_b[`iv']-invttail(e(df_r),.95)*_se[`iv']) (_b[`iv']+invttail(e(df_r),.95)*_se[`iv']) (e(N))

****** Panel


			quietly: xtreg `dv' `iv' /// outcome and IV
				i.citizen i.unemployed income_log i.civil_status i.living_status years_at_address education_years i.counter_panel /// time varying individual controls
				tot_neighbors_`d' agespread_`d' median_income_`d' unempl_narrow_`d' residential_turnover_`d' ethnic_diversity_`d' single_parents_`d' /// contextual controls
				if sample==1 & p2ws==1, /// sample restrictions
					fe ///
					cluster(id)
					
			tempvar countid
			egen `countid'=count(id) if e(sample)==1, by(id)
		
			xtreg `dv' `iv' /// outcome and IV
				i.citizen i.unemployed income_log i.civil_status i.living_status years_at_address education_years i.counter_panel /// time varying individual controls
				tot_neighbors_`d' agespread_`d' median_income_`d' unempl_narrow_`d' residential_turnover_`d' ethnic_diversity_`d' single_parents_`d' /// contextual controls
				if sample==1 & p2ws==1 & `countid'==2, /// sample restrictions
					fe ///
					cluster(id)
				
		
			frame post output ("`dv'") ("`iv'") ("Panel") (`d') (_b[`iv']) (_se[`iv']) (_b[`iv']-invttail(e(df_r),.975)*_se[`iv']) (_b[`iv']+invttail(e(df_r),.975)*_se[`iv']) (_b[`iv']-invttail(e(df_r),.95)*_se[`iv']) (_b[`iv']+invttail(e(df_r),.95)*_se[`iv']) (e(N))
	
			
		}
	
	}
			
}

log close

